<?php
require "genDotFile.php";

	function pre_request(){
		print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html>
		    <head>
				<meta charset="utf-8" />
				<title>ggcDiag</title>
				<script src="jquery-1.10.2.min.js"></script>
				<script>
					$(document).ready(function(){
						$("#btn_start_ggcd").on("click", function() {
							if(typeof($("#upfile").get(0).files[0])=="undefined") {
								alert("first select a xml file generated by gccxml.");
								return;
							}
							var fd = new FormData();
							fd.append("upload", 1);
							fd.append("upfile", $("#upfile").get(0).files[0]);
							$.ajax({
								url: "upload_file.php",
								type: "POST",
								processData: false,
								contentType: false,
								data: fd,
								success: function(d) {
									console.log(d);
									
									if(d != "OK!") {
										$("#i_fanl_rs").html("err<br/>" + d);	
										return;
									}
									$.post(
											"index.php",
											{
												name:"ggcDiag",
											},
											function(dat, status) {
												if("OK!" == dat) {
													$("#i_fanl_rs").html("<img src=122.png />");	
												} else {
													$("#i_fanl_rs").html("err<br/>" + dat);	
												}										
											}
									);
								}
							});
						});
						
					});
				</script> 
						
<style type="text/css">
#customers
  {
  font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
  width:100%;
  border-collapse:collapse;
  }

#customers td, #customers th 
  {
  font-size:1em;
  border:1px solid #98bf21;
  padding:3px 7px 2px 7px;
  }

#customers th 
  {
  font-size:1.1em;
  text-align:left;
  padding-top:5px;
  padding-bottom:4px;
  background-color:#A7C942;
  color:#ffffff;
  }

#customers tr.alt td 
  {
  color:#000000;
  background-color:#EAF2D3;
  }
</style>
			</head>
			<body>
				<div id="1">
					<div style="display:inline; ">
						<textarea id="ita1" style="width:20%; height:100px;" title="dot-text"></textarea>
					</div>
					<div id="i_dbug_area" style="display: none; "><textarea id="ita2" style="width:40%; height:300px;"></textarea></div><br/>
				</div>

				<p><input type="file" id="upfile"></p>
				<p><input type="button" id="btn_start_ggcd" value="upLoad & generate diagrame"></p>

				<div id="i_fanl_rs"></div>
			</body>
		        
		';
	}
	
	if (isset($_POST["name"]) && "ggcDiag" == $_POST["name"]) {
		gen_dot_file("upload\\demo.xml");
		//$rt = system("\"C:\\Program Files\\Graphviz2.38\\bin\\dot.exe\" " . "upload\\" . $_POST["fn"] . ".dot -Tpng -o 122.png", $out);
		$rt = system("\"C:\\Program Files\\Graphviz2.38\\bin\\dot.exe\" upload\\demo.xml.dot -Tpng -o 122.png", $out);
		if(0 == $out) {
			print "OK!";
		} else {
			print "dot file process err:" . $out;
		}
	} else {
		pre_request();	
	}

?>